<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 文章模型
 */
class Course extends ModelBase
{
    protected $insert = ['create_time'];

    public function getImageAttr(){
        return get_picture_url($this->cover_id,false);
    }




    /**
     * 连用户表
     */
    public function user(){
        return $this->belongsTo('Member','member_id','id');
    }

    /**
     * 连老师表
     */
    public function teacher(){
        return $this->belongsTo('CourseTeacher','teacher_id','id');
    }

    /**
     * 连大纲表
     */
    public function collect(){
        return $this->hasMany('CourseCollections','course_id','id');
    }


    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }


    /**
     * 学习人数
     * @return int
     */
    public function GetLearnerCountAttr(){
        return    CourseJoin::where(['course_id'=>$this->id])->count();
    }


    public function  is_buy($uid){
        return   CourseJoin::where(['course_id'=>$this->id,'user_id'=>$uid])->count();
    }


    /**
     * 更行集数
     * @return int
     */
    public function GetRenewAttr(){
        return   count($this->collect);
    }


    /**
     * 获取状态信息
     */
    public function getTypeTextAttr(){

        $arr=[ '','视频','音频','直播'];
        return lang($arr[$this->data['type']]);
    }


    public function  getDescribeAttr($value){
        if(isset($_REQUEST['lang']) && $_REQUEST['lang']!='zh-cn' &&  $_REQUEST['lang'] ){
            return   baidu_translate($value);
        }

        return $value;

    }

    public function  getContentAttr($value){
        if(isset($_REQUEST['lang']) && $_REQUEST['lang']!='zh-cn' &&  $_REQUEST['lang'] ){
            return   baidu_translate($value);
        }
        return $value;

    }
}
